package com.example.dzbsh.controller;

import com.example.dzbsh.model.ChaheQingkuang;
import com.example.dzbsh.model.JingshangQingkuang;
import com.example.dzbsh.service.ChaheQingkuangService;
import com.example.dzbsh.service.JingshangQingkuangService;
import com.example.dzbsh.util.JsonResult;
import com.example.dzbsh.util.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("chaheqingkuang")
public class ChaheQingkuangController {

    @Autowired
    ChaheQingkuangService ddService;
    @Autowired
    JingshangQingkuangService ddJingshangQingkuangService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    //跟据单位查询管理员
    @RequestMapping("find_by_deptid_and_jiashuname_and_shifoubangding")
    public JsonResult<?> findByDeptIdAndJiashunameAndShifoubangding(@RequestParam("deptid") String ppDeptId,@RequestParam("jiashuname") String ppJiashuname, @RequestParam("shifoubangding") String ppShifoubangding,
                                                                    @RequestParam("startindex") int ppStartIndex, @RequestParam("pagesize") int ppPageSize) {
        try {
            List<Map<String,Object>> mmList = ddService.findByDeptIdAndJiashunameAndShifoubangding(ppDeptId,ppJiashuname,ppShifoubangding,ppStartIndex,ppPageSize);
            Integer mmCount = ddService.getCountByDeptIdAndJiashunameAndShifoubangding(ppDeptId,ppJiashuname,ppShifoubangding,ppStartIndex,ppPageSize);

            PageInfo mmPageInfo = new PageInfo(ppStartIndex, ppPageSize, mmCount);

            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("ChaheList", mmList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ChaheQingkuangController->findByDeptIdAndJiashunameAndShifoubangding:"+e.getMessage());
            return JsonResult.getErrorResult("根据单位查询单位管理员失败");
        }
    }

    @RequestMapping("bangding")
    public JsonResult<?> bangding(@RequestParam("chaheid") String ppChaheId,@RequestParam("jingshangid") String ppJingshangId) {
        try {
            ChaheQingkuang mmChaheQingkuang = ddService.findOne(ppChaheId);
            if (mmChaheQingkuang==null){
                return  JsonResult.getErrorResult("暂无该查核情况");
            }

            String mmJingshangId = mmChaheQingkuang.getJingshangid() == null ? "" : mmChaheQingkuang.getJingshangid();
            if (!"".equals(mmJingshangId)){
                return  JsonResult.getErrorResult("该查核情况已绑定经商情况，请勿重复绑定");
            }
            JingshangQingkuang mmJingshangQingkuang = ddJingshangQingkuangService.findOne(ppJingshangId);
            if (mmJingshangQingkuang==null){
                return  JsonResult.getErrorResult("不存在该经商情况");
            }
            String mmRenyuanId = mmJingshangQingkuang.getRenyuanid() == null ? "" : mmJingshangQingkuang.getRenyuanid();
            String mmJiashuId = mmJingshangQingkuang.getJiashuid() == null ? "" : mmJingshangQingkuang.getJiashuid();


            mmChaheQingkuang.setJingshangid(ppJingshangId);
            mmChaheQingkuang.setRenyuanid(mmRenyuanId);
            mmChaheQingkuang.setJiashuid(mmJiashuId);
            return ddService.modify(mmChaheQingkuang) == true ? JsonResult.getSuccessResult("绑定查核信息成功")
                    : JsonResult.getErrorResult("绑定查核信息失败");
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("chaheqingkuang/bangding:error " + e.getMessage());
        }
    }

    @RequestMapping("findnumbydeptid")
    public JsonResult<?> findNumByDeptId(@RequestParam("deptid") String ppDeptId,@RequestParam("isall") boolean ppIsAll) {
        try {
            Map<String,Object> mmRequest = new HashMap<>();
            mmRequest.put("deptchahenum",ddService.findCaheNumByDeptId(ppDeptId,ppIsAll));
            mmRequest.put("deptweibangding",ddService.findWeiBnagdingCaheNumByDeptIdList(ppDeptId,ppIsAll));
            return  JsonResult.getSuccessResult(mmRequest);
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("chaheqingkuang/findnumbydeptid:error " + e.getMessage());
        }
    }

    @RequestMapping("findxitongnum")
    public JsonResult<?> findXitongNum() {
        try {
            Map<String,Object> mmRequest = new HashMap<>();
            mmRequest.put("xitongchahenum",ddService.findByChaheNum());
            mmRequest.put("xitongweibangding",ddService.findWeiBnagdingCaheNum());
            return  JsonResult.getSuccessResult(mmRequest);
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("chaheqingkuang/findxitongnum:error " + e.getMessage());
        }
    }

    @RequestMapping("find_by_jiashuid")
    public JsonResult<?> findByJiashuid(@RequestParam("jiashuid") String ppJiashuId) {
        try {

            return  JsonResult.getSuccessResult(ddService.findByJiashuId(ppJiashuId));
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("chaheqingkuang/findByJiashuid:error " + e.getMessage());
        }
    }
}
